﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThuongLMS.ViewModels;
//Author: Kha
namespace VietThuongLMS.Services
{
    public class ChuongTrinhKhuyenMaiService
    {
        public static List<ChuongTrinhKhuyenMaiMoRongModel> GetAllDiscounts()
        {  
            var context = new LMSDataEntities();
            try
            {
                var dl = (from d in context.ChuongTrinhKhuyenMais where d.Deleted == 0 
                          select new ChuongTrinhKhuyenMaiMoRongModel {CTKM = d, HocPhiGoc = d.LoaiLopHoc.HocPhi,
                                                                      MoTa = d.LoaiLopHoc.MoTa, TenLoaiLopHoc = d.LoaiLopHoc.TenLoaiLopHoc,
                                                                      ThoiLuong = d.LoaiLopHoc.ThoiLuong }).OrderByDescending(ct => ct.CTKM.NgayBatDau).ToList();
                return dl;
            }
            catch (Exception ex) {    
                DCS.ComLib.File.Log.Write("DiscountsServices::GetAllDiscounts", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {             
                context.Dispose();
            }
        }

        public static ChuongTrinhKhuyenMaiViewModel GetAllDiscountsWithPagination(int pageNum, int pageIndex)
        {
            var context = new LMSDataEntities();
            try
            {
                var dl = (from d in context.ChuongTrinhKhuyenMais
                          where d.Deleted == 0
                          select new ChuongTrinhKhuyenMaiMoRongModel
                          {
                              CTKM = d,
                              HocPhiGoc = d.LoaiLopHoc.HocPhi,
                              MoTa = d.LoaiLopHoc.MoTa,
                              TenLoaiLopHoc = d.LoaiLopHoc.TenLoaiLopHoc,
                              ThoiLuong = d.LoaiLopHoc.ThoiLuong
                              
                          }).OrderByDescending(ct => ct.CTKM.NgayBatDau).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.ChuongTrinhKhuyenMais.Count();
                ChuongTrinhKhuyenMaiViewModel ctkmvm = new ChuongTrinhKhuyenMaiViewModel(dl, total);
                ctkmvm.TinhHocPhiSauKhiGiam();
                return ctkmvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiscountsServices::GetAllDiscountsWithPagination", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static ChuongTrinhKhuyenMai GetChuongTrinhKhuyenMaiByID(int id)
        {
            var context = new LMSDataEntities();
            try
            {
                var ctkm = (from c in context.ChuongTrinhKhuyenMais where c.ID == id select c).First();
                return ctkm;
            }
            catch (Exception ex) {
                DCS.ComLib.File.Log.Write("DiscountsServices::GetChuongTrinhKhuyenMaiByID", ex.StackTrace, ex.Message);
                throw ex; 
            }
            finally
            {
                context.Dispose();
            }
        }
        public static Boolean UpdateChuongTrinhKhuyenMai(ChuongTrinhKhuyenMai ctmk)
        {
            var context = new LMSDataEntities();

            try
            {
                var upctmk = context.ChuongTrinhKhuyenMais.Where(p => p.ID == ctmk.ID).FirstOrDefault();
                upctmk.GiamGia = ctmk.GiamGia;
                upctmk.MaLoaiLopHoc = ctmk.MaLoaiLopHoc;
                upctmk.NgayBatDau = ctmk.NgayBatDau;
                upctmk.NgayKetThuc = ctmk.NgayKetThuc;
                upctmk.TenChuongTrinh = ctmk.TenChuongTrinh;
                context.SaveChanges();
                return true;
            }
            catch (Exception ex) {
                DCS.ComLib.File.Log.Write("DiscountsServices::UpdateChuongTrinhKhuyenMai", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        public static Boolean Insert(ChuongTrinhKhuyenMai ctkm)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                var newct = new ChuongTrinhKhuyenMai();
                newct.Deleted = 0;
                newct.TenChuongTrinh = ctkm.TenChuongTrinh;
                newct.MaLoaiLopHoc = ctkm.MaLoaiLopHoc;
                newct.GiamGia = ctkm.GiamGia;
                newct.NgayBatDau = Convert.ToDateTime(ctkm.NgayBatDau);
                newct.NgayKetThuc = Convert.ToDateTime(ctkm.NgayKetThuc);
                context.AddToChuongTrinhKhuyenMais(newct);
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiscountsServices::Insert", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        public static Boolean UpdateDeleteFiedChuongTrinhKhuyenMai(int id)
        {
            var context = new LMSDataEntities();

            try
            {
                var upctmk = context.ChuongTrinhKhuyenMais.Where(p => p.ID == id).FirstOrDefault();
                upctmk.Deleted = 1;
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("DiscountsServices::UpdateDeleteFiedChuongTrinhKhuyenMai", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
    }
}